package com.ee.chat.admin.service2.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service2.IDataAnalysisService;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.DataAnalysisLogMapper;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author AppiJ
 * @Date 2020/10/13 20:59
 */
@Service
public class DataAnalysisServiceImpl implements IDataAnalysisService {

    @Resource
    DataAnalysisLogMapper dataAnalysisLogMapper;
    @Resource
    IOperationService operationService;

    @Override
    public Result list(HttpServletRequest request, String time, Integer page, Integer limit) throws Exception {
        Map<String, Object> data = new HashMap<>();

        if(StringUtil.isEmpty(time)){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/dataAnalysis/viewList")){
                data.put("datas", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        Date beginTime = null, endTime = null;
        if(!StringUtil.isEmpty(time)){
            String[] arr = time.split(" - ");
            beginTime = DateUtil.parse(arr[0]);
            endTime = DateUtil.parse(arr[1]);
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = dataAnalysisLogMapper.list(p, beginTime, endTime);
        List<Map<String, Object>> maps = iPage.getRecords();

        data.put("datas", maps);
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "获取数据分析列表数据");

        return Result.returnSuccess(data);
    }

}
